/**
 * DBConnection.java
 * org.opensmile.study.db
 *
 * Function： TODO 
 *
 *   ver     date      		author
 * ──────────────────────────────────
 *   		 2011-11-15 		opensmile
 *
 * Copyright (c) 2011, 潮流网络有限公司杭州分公司(www.grandstream.com.cn).
*/

package org.opensmile.study.db;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.concurrent.TimeoutException;

import net.rubyeye.xmemcached.MemcachedClient;
import net.rubyeye.xmemcached.MemcachedClientBuilder;
import net.rubyeye.xmemcached.XMemcachedClientBuilder;
import net.rubyeye.xmemcached.exception.MemcachedException;

import org.opensmile.study.domain.Sys_viewUsefulAdvProbability;





/**
 * ClassName:DBConnection
 * @Description: TODO(这里用一句话描述这个类的作用) 
 * @author   opensmile
 * @version 1.0 2011-11-15
 * @since 1.0
 * @see
 */
public class DBConnection {
	
	private final static String url ="jdbc:mysql://192.168.120.241/website_core_test?useUnicode=true&characterEncoding=utf-8";
	
	
	private final static String user="test";
	
	private final static String password="test";
	
	
	public static Connection getConnection(){
		Connection conn=null;
		try {
			Class.forName("com.mysql.jdbc.Driver");
			conn =DriverManager.getConnection(url, user, password);
		} catch (ClassNotFoundException e) {
			
			// TODO Auto-generated catch block
			e.printStackTrace();
			
		} catch (SQLException e) {
			
			// TODO Auto-generated catch block
			e.printStackTrace();
			
		}
		return conn;
	}
	
	
	public static ResultSet getResult(String sql ){
		Statement stmt =null;
		ResultSet rset =null;
		Connection conn =null;
		try {
			conn =getConnection();
			stmt =conn.createStatement();
			rset =stmt.executeQuery(sql);
			while(rset.next()){
				String key =rset.getString(1);
				String value =rset.getString(2);
				userMemcache(key,value);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			if(stmt!=null){
				try {
					stmt.close();
				} catch (SQLException e) {
					
					// TODO Auto-generated catch block
					e.printStackTrace();
					
				}
			}
			if(conn!=null){
				try {
					conn.close();
				} catch (SQLException e) {
					
					// TODO Auto-generated catch block
					e.printStackTrace();
					
				}
			}
		}
		return rset;
	}
	
	
	public static void userMemcache(String key,String value){
		InetSocketAddress sockAddress =new InetSocketAddress("192.168.120.241", 11211);
		java.util.List sockList =new ArrayList<InetSocketAddress>();
		sockList.add(sockAddress);
		
		MemcachedClientBuilder builder =new XMemcachedClientBuilder(sockList);
		
		try {
			MemcachedClient memcachedClient =builder.build();
			memcachedClient.set("viewsql"+key, 0, value);
			System.out.println(key +" : "+value);
			memcachedClient.shutdown();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (TimeoutException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (MemcachedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	
}

